package com.yupi.springbootinit.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @Description
 * @Author minify
 * @Date 2023/12/16 14:45
 * @Project mybi-backend
 */
@Configuration
public class ThreadPoolExecutorConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        // 核心线程池数
        int corePoolSize = 2;
        // 最大线程池数
        int maximumPoolSize = 4;
        // 线程最大空闲时间
        long keepAliveTime = 180;
        // 时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        // 线程等待队列
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(20);
        // 线程创建工厂
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        // 拒绝策略
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }
}
